﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MLY.Bll
{
  public class BasePage : System.Web.UI.Page
  {
    protected void showErrorMsg(string msg)
    {
      string script = string.Format(@"<script language='javascript'> alert('{0}')</script>", msg);
      this.ClientScript.RegisterStartupScript(this.GetType(), "errorScript", script);
    }

    public void AttachClicked(object sender, ImageClickEventArgs e)
    {
      string woNumber = ((ImageButton)sender).CommandArgument;
      string script = String.Format(@"<script language ='javascript'>window.open('AttachmentManager.aspx?woId={0}','width=200, height=77')</script>", woNumber);
      if (HttpContext.Current.Handler is Page)
        ((Page)HttpContext.Current.Handler).ClientScript.RegisterStartupScript(((Page)HttpContext.Current.Handler).GetType(), "myScript", script);
    }

    protected override void OnError(EventArgs e)
    {
      // At this point we have information about the error
      HttpContext httpContext = HttpContext.Current;

      Exception exception = httpContext.Server.GetLastError();
      if (exception != null)
      {
        string errorInfo =
           "<br>Source: " + exception.Source +
           "<br>Message: " + exception.Message +
           "<br>Stack trace: " + exception.StackTrace;
        Session["error"] = errorInfo;

        Log.OnPageError(httpContext, exception);
      }

      if (!IsPostBack)
      {
        httpContext.Response.RedirectTo(string.Format(@"/customErrors.aspx?ReturnTo={0}", httpContext.Request.Url.ToString()));
        // --------------------------------------------------
        // To let the page finish running we clear the error
        // --------------------------------------------------
        httpContext.Server.ClearError();
      }
      base.OnError(e);
    }
  }
}
